/* GUI CONVERTED from rescrapa.p (converter v1.76) Fri Sep 28 01:37:56 2001 */
/* rescrapa.p - REPETITIVE                                                    */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.10.2.11 $                                                         */
/*V8:ConvertMode=Maintenance                                                  */
/* REVISION: 7.3      LAST MODIFIED: 10/31/94   BY: WUG *GN77*                */
/* REVISION: 7.3      LAST MODIFIED: 06/24/96   BY: *G1XX* Julie Milligan     */
/* REVISION: 7.3      LAST MODIFIED: 07/25/97   BY: *G2LF* Julie Milligan     */
/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane          */
/* REVISION: 8.6E     LAST MODIFIED: 03/20/98   BY: *J23R* Santhosh Nair      */
/* REVISION: 8.6E     LAST MODIFIED: 10/04/98   BY: *J314* Alfred Tan         */
/* REVISION: 9.1      LAST MODIFIED: 03/15/99   BY: *N00J* Russ Witt          */
/* REVISION: 9.1      LAST MODIFIED: 10/01/99   BY: *N014* Jeff Wootton       */
/* REVISION: 9.1      LAST MODIFIED: 11/17/99   BY: *N04H* Vivek Gogte        */
/* REVISION: 9.1      LAST MODIFIED: 08/29/00   BY: *N0PN* Mark Brown         */
/* REVISION: 9.1      LAST MODIFIED: 09/22/00   BY: *L0Y1* Kirti Desai        */
/* REVISION: 9.1      LAST MODIFIED: 11/13/00   BY: *N0TN* Jean Miller      */
/* Old ECO marker removed, but no ECO header exists *F0PN*               */
/* $Revision: 1.10.2.11 $    BY: Irine D'Mello  DATE: 09/10/01  ECO: *M164*     */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

/* SCRAP TRANSACTION REGISTER SCRAP SUBPROGRAM                                */

{mfdeclre.i}
{gplabel.i}
{pxmaint.i}

{pxphdef.i wocmnrtn}

define input parameter cumwo_lot as character.
define input parameter op as integer.
define input parameter qty_scrap as decimal.
define input parameter rsn_code as character.
define input parameter output_reject like mfc_logical.
define input parameter emp as character.
define input parameter ophist_recid as recid.

define new shared variable site as character.
define new shared variable comp as character.
define new shared variable wo_recno as recid.
define new shared variable use_op_yield  as logical no-undo.

define variable wip_qty as decimal.
define variable new_wip_qty as decimal.
define variable qty_open as decimal no-undo.
define variable qty_left as decimal no-undo.
define variable qty_to_apply as decimal no-undo.
define variable part like wo_part no-undo.
define variable line like wo_line no-undo.
define variable pointer_date as date no-undo.
define variable yield_pct  like pt_yield_pct no-undo.
define variable v-op       like ro_op        no-undo.
define variable l_errorno  as   integer      no-undo.

define new shared buffer gl_ctrl for gl_ctrl.
define new shared temp-table tt-routing-yields no-undo
   field tt-routing    like ro_routing
   field tt-op         like ro_op
   field tt-start      like ro_start
   field tt-end        like ro_end
   field tt-yield-pct  like ro_yield_pct
   index tt-routing is unique primary
   tt-routing
   tt-op
   tt-start.

if qty_scrap = 0
then
   leave.

for first mrpc_ctrl
   fields (mrpc_op_yield)
   no-lock:
end.

find wo_mstr
   where wo_lot = cumwo_lot no-lock.

do transaction:
/*GUI*/ if global-beam-me-up then undo, leave.


   find wr_route
      where wr_lot = cumwo_lot
        and wr_op  = op
      exclusive-lock.

   if output_reject = yes
   then do:

      /*SCRAP FROM OUTPUT QUE*/
      assign
         wr_qty_outque = wr_qty_outque - qty_scrap
         wr_qty_cumoscrap = wr_qty_cumoscrap + qty_scrap.

      /*UPDATE REPETITIVE SCHEDULE*/
      {gprun.i ""reupdsch.p""
         "(input cumwo_lot, input op, input (- qty_scrap))"}
/*GUI*/ if global-beam-me-up then undo, leave.

   end.

   else
      /*SCRAP FROM REJECT QUE*/
      assign
         wr_qty_rejque = wr_qty_rejque - qty_scrap
         wr_qty_cumrscrap = wr_qty_cumrscrap + qty_scrap.

   find op_hist
      where recid(op_hist) = ophist_recid exclusive-lock.

   assign
      op_rsn = rsn_code
      op__dec01 = qty_scrap.

   /* GET THE STARTING DATE TO CONSUM SCRAP FROM */
   find qad_wkfl no-lock
      where qad_key1 = "rps_mstr"
        and qad_key2 = wo_part + "/" + wo_site + "/" + wo_line
      no-error.

   if available qad_wkfl
   then
      pointer_date = qad_datefld[1].

   /* ADJUST THE SCHEDULE */

   assign
      qty_left = qty_scrap
      part = wo_part
      site = wo_site
      line = wo_line.

   /* WHEN THE qty_scrap > 0, THEN CONSUME STARTING   */
   /* WITH THE EARLIEST SCHDULE                       */

   if qty_scrap > 0
   then do:

      if pointer_date = ?
      then
         pointer_date = low_date.

      for each wo_mstr
         where wo_part = part
           and wo_site = site
           and wo_line = line
           and wo_type = "S"
           and wo_due_date >= pointer_date
         use-index wo_type_part:
/*GUI*/ if global-beam-me-up then undo, leave.


         find rps_mstr exclusive-lock
            where rps_part = wo_part
              and rps_due_date = wo_due_date
              and rps_line = wo_line
              and rps_site = wo_site no-error.

         if available rps_mstr
         then do:

            assign
               qty_to_apply = max(0,(min(rps_qty_req - rps_qty_comp,qty_left)))
               rps_qty_comp = rps_qty_comp + qty_to_apply
               qty_left = qty_left - qty_to_apply
               qty_open = max(0,(rps_qty_req - rps_qty_comp))
               wo_qty_comp = wo_qty_comp + qty_to_apply.

            {mfmrw.i "wo_mstr" wo_part wo_nbr wo_lot """"
               wo_rel_date wo_due_date
               qty_open
               "SUPPLYF" " " wo_site}

            /* Changed pre-processor to Term: SCRAP_REQUIREMENT */
            {mfmrw.i "wo_scrap" wo_part wo_nbr wo_lot """"
               wo_rel_date wo_due_date
               "qty_open * (1 - wo_yield_pct / 100)"
               "DEMAND" SCRAP_REQUIREMENT wo_site}

         end.

         if qty_left = 0
         then
            leave.

      end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* for each wo_mstr */

   end. /* qty_scrap > 0*/

   /* WHEN qty_scrap < 0 THEN RESTORE CONSUMPTION STARTING WITH THE     */
   /* LATEST SCHDULE                                                    */

   else do:

      if pointer_date = ?
      then
         pointer_date = hi_date.

      /* DRIVE THE REVERSAL BY USING THE RPS_MSTR    */
      /* AS THE SCHEDULED WO_MSTR MAY HAVE BEEN      */
      /* DELETED IF THIS SCHEDULE WAS RE-EXPLOED     */
      /* AFTER A SCHEDULE WO_MSTR WAS FULLY CONSUMED.*/

      for each rps_mstr
         where rps_part = part
           and rps_site = site
           and rps_due_date <= pointer_date
           and rps_line = line
           and rps_qty_comp > 0
         use-index rps_part
         by rps_part descending
         by rps_site descending
         by rps_line descending
         by rps_due_date descending:
/*GUI*/ if global-beam-me-up then undo, leave.


         /* NOW FIND THE CORRESPONDING WO_MSTR, IF IT DOES NOT EXISTS */
         /* THEN CREATE IT AND THE CORRESPONDING WOD_DET              */
         find wo_mstr exclusive-lock /* MRS - Changed no-lock to excl */
            where wo_part     = rps_part
              and wo_site     = rps_site
              and wo_due_date = rps_due_date
              and wo_lot      = string(rps_record) /* MRS */
              and wo_type     = "s" no-error.

         if not available wo_mstr
         then do:

            create wo_mstr.
            assign
               wo_lot = string(rps_record)
               wo_part = rps_part
               wo_nbr = rps_part
               wo_type = "S"
               wo_status = "E"
               wo_line = rps_line
               wo_site = rps_site
               wo_ord_date = today
               wo_rel_date = rps_rel_date
               wo_due_date = rps_due_date
               wo_routing = if rps_routing <> ""
                            then
                               rps_routing
                            else
                               ""
               wo_bom_code = if rps_bom_code <> ""
                             then
                                rps_bom_code
                             else
                                ""
               wo_qty_ord = rps_qty_req
               wo_qty_comp = rps_qty_req.

            if recid(wo_mstr) = -1 then .

            /* UPDATE THE WORK ORDER ACCOUNTS */
            find pt_mstr no-lock
               where pt_part = rps_part no-error.

            if available pt_mstr
            then do:
               /* Determine yield percentage to be used                     */
               yield_pct = 100.
               for first ptp_det
                  fields(ptp_part ptp_site ptp_op_yield ptp_yld_pct)
                  where ptp_part = rps_part
                    and ptp_site = rps_site
                  no-lock:
               end.

               if available ptp_det
               then
                  assign
                     use_op_yield = ptp_op_yield
                     yield_pct = ptp_yld_pct.
               else
                  assign
                     use_op_yield = pt_op_yield
                     yield_pct = pt_yield_pct.

               /* CHECK FOR OPERATION YIELD USE  */
               if available mrpc_ctrl
                  and mrpc_op_yield = yes
                  and use_op_yield = yes
               then do:

                  /* Load temp table with this item's routing data...  */
                  run ip-load-routing-temp-table
                     (input if rps_routing <> "" then rps_routing
                     else rps_part).

                  /* Calculate yield... */
                  /* Pass all 9's for all operations are used... */
                  v-op = 999999999.
                  run ip-get-yield
                     (input if rps_routing <> ""
                            then
                               rps_routing
                            else
                               rps_part,
                     input v-op,
                     input rps_rel_date,
                     output yield_pct).

               end.  /* if available mrpc_ctrl... */
               wo_yield_pct = yield_pct.

               /* ASSIGN DEFAULT RECEIPT STATUS AND ACTIVE FLAG */
               {pxrun.i &PROC    = 'get_default_wo_rctstat'
                  &PROGRAM = 'wocmnrtn.p'
                  &HANDLE  = ph_wocmnrtn
                  &PARAM   = "(
                               input  wo_part,
                               input  wo_site,
                               output wo_rctstat,
                               output wo_rctstat_active,
                               output l_errorno
                              )"
               }

               /* ASSIGN DEFAULT VARIANCE ACCT SUB-ACCT AND COST CENTER CODE. */
               run assign_default_wo_acct(buffer wo_mstr,
                                          input  pt_prod_line).

            end. /* IF AVAILABLE PT_MSTR */

            if recid(wo_mstr) = -1 then .
            assign
               site = rps_site
               comp = rps_part.

            /* IF AN ALTERNATE BOM IS BEING USED, USE THAT INSTEAD OF */
            /* THE PART FOR EXPLODE ITEM TO GET COMPONENT LIST.       */
            if rps_bom_code <> ""
            then
               comp = rps_bom_code.
            {gprun.i ""rerpexb.p""}
/*GUI*/ if global-beam-me-up then undo, leave.


            /*RECALCULATE COMPONENT DEMAND FOR MRP*/
            find pt_mstr
               where pt_part = rps_part no-lock.

            assign
               wo_recno = recid(wo_mstr)
               pt_recno = recid(pt_mstr).

            {gprun.i ""rerpexa.p""}
/*GUI*/ if global-beam-me-up then undo, leave.


         end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* need to create wo_mstr */

         assign
            qty_to_apply = max(qty_left, - rps_qty_comp)
            rps_qty_comp = rps_qty_comp + qty_to_apply
            qty_open = max(0,(rps_qty_req - rps_qty_comp))
            qty_left = qty_left - qty_to_apply
            wo_qty_comp = wo_qty_comp + qty_to_apply.

         {mfmrw.i "wo_mstr" wo_part wo_nbr wo_lot """"
            wo_rel_date wo_due_date
            qty_open
            "SUPPLYF" " " wo_site}

         /* Changed pre-processor to Term: SCRAP_REQUIREMENT */
         {mfmrw.i "wo_scrap" wo_part wo_nbr wo_lot """"
            wo_rel_date wo_due_date
            "qty_open * (1 - wo_yield_pct / 100)"
            "DEMAND" SCRAP_REQUIREMENT wo_site}

         if qty_left = 0 then leave.
      end. /* for each rps_mstr */
   end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* qty_scrap < 0 */

end.

/******************************************************************/

/*   I N T E R N A L    P R O C E D U R E S     */

/******************************************************************/

/* This routine will load routing information  for a part into  */
/* a temp table.                                                */
{gplodyld.i}

/******************************************************************/

/* This routine will determine operation yield percentage        */
/* used.                                                         */
{gpgetyld.i}

/******************************************************************/

/* INCLUDE FILE WHICH CONTAINS PROCEDURE assign_default_wo_acct */
{woacct.i}
